package com.interfaces;

import java.util.List;

import javax.transaction.Transactional;

import org.springframework.data.jpa.repository.JpaRepository;
import org.springframework.data.jpa.repository.Modifying;
import org.springframework.data.jpa.repository.Query;

import com.entity.Recharge;
import com.entity.TaskRecord;

/**
 * @Title: RechargeDao.java
 * @Package com.interfaces
 * @author 孙梦楠
 * @date 2017年8月22日 下午12:57:17
 * @version V1.0.0
 */

public interface RechargeDao extends JpaRepository<Recharge, Integer> {
	// 正常列表
	List<Recharge> findByCustomerid(Integer customerid);

	// 降序列表
	List<Recharge> findByCustomeridAndDirectionAndFrequencyNotOrderByFrequencyDesc(Integer customerid, String direction,Integer frequency);

	int deleteByCustomerid(Integer customerid);

	// 工单状态：0 等待下发, 1 下发成功，2 下发失败，3 正在下发
	List<Recharge> findByOptstatusOrderByRechargeid(String optstatus);
	
	@Transactional
	@Query(value="SELECT * FROM e_recharge r WHERE r.opt_status=\"等待下发\"OR r.opt_status=\"正在下发\"  GROUP BY r.meter_id",nativeQuery=true)
	List<Recharge> findByOptstatusGroupByMeterid();

//	// 销户列表
//	@Transactional
//	@Query(value = "SELECT r FROM Recharge r WHERE (r.customeraccount=?1 OR ?1 IS NULL )"
//			+ "AND (r.customername=?2 OR ?2 IS NULL )" + "AND (r.customeraddress=?3 OR ?3 IS NULL)"
//			+ "AND (r.customertype=?4)" + "AND (r.customerorg=?5)" + "AND (r.direction like %?6) ")
//	List<Recharge> findByCondition(String customeraccount, String customername,
//			String customeraddress, String customertype, Integer customerorg, String direction);

	// 更改状态
	// 更新表状态 实际充值时间，执行前金额，执行后金额，累计购电金额，工单状态，操作次数
	@Transactional
	@Modifying
	@Query("update Recharge r set r.rechargetime = ?1,r.beforemoney = ?2 , r.aftermoney = ?3,r.totalmoney=?4,"
			+ "r.optstatus=?5,r.optnum=?6 ,r.remark=?7 where r.rechargeid = ?8")
	int updateRecharge(String rechargetime, Double beforemoney, Double aftermoney, Double totalmoney,
			String optstatus, Integer optnum, String remark,Integer rechargeid);
	
	@Transactional
	@Modifying
	@Query("update Recharge r set r.direction=?1 where r.meterid=?3 and r.direction=?2")
	int updateDirection(String newdirection,String olddirection,Integer meterid );
	
	@Transactional
	@Modifying
	@Query("update Recharge r set r.remark=?1 where r.meterid=?2")
	int updateRemark(String remark,Integer meterid );
	
	//报表，通过日期查记录
	@Transactional
	@Query(value="SELECT r FROM Recharge r WHERE  (r.issuetime > ?1 OR  ?1 IS NULL) AND (r.issuetime < ?2 OR ?2 IS NULL) "
			+ "AND r.opecode=?3 AND r.direction=?4 AND r.frequency != 0") 
	public List<Recharge> listRechargeReport(String issuetime1,String issuetime2,String oprcode,String direction);

}
